package com.service.impl;

import com.dao.StudentDao;
import com.domain.College;
import com.domain.PageBean;
import com.domain.SelectCourse;
import com.domain.Student;
import com.service.CourseService;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    private int pageSize = 6;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private CourseService courseService;
    /**
     * 选课
     * @return
     */
    public String selectCourse(String userName,SelectCourse sCourse){
        List<SelectCourse> courses = courseService.findCourseByName(Integer.parseInt(userName));
        for (SelectCourse course:courses) {
            if (sCourse.getCourseID() == course.getCourseID()){
                return "你已经选过该课程，请重新选择！";
            }
        }
        studentDao.selectCourse(sCourse);
        return "success";
    }
    @Override
    public void outCourse(SelectCourse course) {
        studentDao.outCourse(course);
    }
    @Override
    public Student findById(String id) {
        return studentDao.findById(id);
    }
    @Override
    public int findAllTotal(int studentID) {
        return studentDao.findAllTotal(studentID);
    }
    @Override
    public int findAllOverC(int studentID) {
        return studentDao.findAllOverC(studentID);
    }
    @Override
    public int totalStudent(String name) {
        return studentDao.totalStudent(name);
    }
    //模糊查询学生信息
    @Override
    public PageBean findAllStudent(String name, int currentPage) {
        int total = 0;
        if (name != null) {
            total = studentDao.totalStudent("%" + name + "%");
        }else if(name == null){
            total = studentDao.totalStudent(null);
        }
        int totalPage = 0;  //总页面数
        if (total % pageSize == 0) {
            totalPage = total / pageSize;
        }else {
            totalPage = total / pageSize + 1;
        }
        int start = (currentPage - 1) * pageSize;
        List<College> list = null;
        if (name == null){
            list = studentDao.findAllStudent(null,start,pageSize);
        }else {
            list = studentDao.findAllStudent("%"+name+"%",start,pageSize);
        }
        PageBean bean = new PageBean(pageSize,currentPage,totalPage,start,list);
        return bean;
    }
    //添加用户信息
    @Override
    public void addStudent(Student student) {
        studentDao.addStudent(student);
    }
    //修改学生信息
    @Override
    public void updateStudent(Student student) {
        studentDao.updateStudent(student);
    }
    //删除学生信息
    @Override
    public void deleteStudent(int parseInt) {
        studentDao.deleteStudent(parseInt);
    }
}
